package com.example.servlettemplate.util;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RequestUtil {
    public static Integer getInterger(HttpServletRequest req, String name) {
        String s = req.getParameter(name);
        if (s != null && s.trim().length() > 0) {
            try {
                return Integer.parseInt(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static Double getDouble(HttpServletRequest req, String name){
        String s = req.getParameter(name);
        if (s != null && s.trim().length() > 0) {
            try {
                return Double.parseDouble(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String getString(HttpServletRequest req, String name){
        String s = req.getParameter(name);
        if (s != null && s.trim().length() > 0) {
            try{
                return s;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String getStringWithBlank(HttpServletRequest req, String name){
        return req.getParameter(name);
    }

    public static Date getDate(HttpServletRequest req, String name, String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String s = req.getParameter(name);
        if (s != null && s.trim().length() > 0) {
            try{
                return format.parse(s);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }
}